<?php
/**
 * Created by PhpStorm.
 * User: Antique
 * Date: 2021/12/6
 * Time: 下午 10:04
 */
namespace test\primary;

include 'mate_cookie.php';
include 'sql_action.php';

$mate_cookie = new mate_cookie();
$pd2 = 0;
$flag = false;
$pd = $mate_cookie->mate();
$arr = explode('.', $_FILES['head_img']['name']);
$md5 = $_COOKIE['Token'];
$img_name = md5($_FILES['head_img']['name'] . time());
$suffix =$arr[count($arr) - 1];

if ($suffix == 'jpg' || $suffix == 'png' || $suffix == 'jpeg') {
    $conn = new \mysqli();
    $conn->connect('127.0.0.1', 'root', '123456', 'userinfo');
    if (!empty($conn->connect_error)) {
        echo '数据库开启异常，请重试';
        die;
    }
    sql_action::set_conn($conn);
    $sql = ('update user_info set head_portrait=\'' . $img_name . '\',head_portrait_type=\'' . $suffix . '\' where cookie=\'' . $md5 . '\'');
    $flag = sql_action::Change($sql);
    if ($flag) {
        move_uploaded_file($_FILES['head_img']['tmp_name'], '../img/' . $img_name .'.' .$suffix);
    }
} else {
    $pd2 = 1;
}
?>
<!!DOCTYPE html>
<html>
<script type="text/javascript">
    switch (<?php echo $pd ?>) {
        case 1:
            alert('你还没登陆或者cookie失效');
            window.location.href = 'login.php';
            break;
        case 2:
            alert('cookie匹配失败');
            window.location.href = 'login.php';
            break;
        case 3:
            alert('cookie到期');
            window.location.href = 'login.php';
            break;
    }

    switch (<?php echo $pd2 ?>) {
        case  1:
            alert('你上传的不是规定格式的文件');
            window.location.href = '../user_function.php';
            break;
        case 0:
            alert('上传成功！');
            window.location.href = '../user_function.php';
            break;
        default:
            alert('异常！');
            window.location.href = '../user_function.php';
            break;
    }
</script>
</html>
